package com.haoniu.web.controller.module;

import com.haoniu.common.utils.StringUtils;
import com.haoniu.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.haoniu.common.annotation.Log;
import com.haoniu.common.enums.BusinessType;
import com.haoniu.module.domain.database.UserWalletIn;
import com.haoniu.module.service.UserWalletInService;
import com.haoniu.web.core.base.BaseController;
import com.haoniu.framework.web.page.TableDataInfo;
import com.haoniu.common.base.AjaxResult;

import java.util.List;
/**
 * 充值 信息操作处理
 *
 * @author houyijun
 * @date 2019-01-25
 */
@Controller
@RequestMapping("/module/userWalletIn")
public class UserWalletInController extends BaseController
{
    private String prefix = "module/userWalletIn";

	@Autowired
	private UserWalletInService userWalletInService;

	@RequiresPermissions("module:userWalletIn:view")
	@GetMapping()
	public String userWalletIn(ModelMap mmap)
	{
		mmap.put("totalChargeMoney",userWalletInService.totalChargeMoney());
	    return prefix + "/userWalletIn";
	}

	/**
	 * 查询充值列表
	 */
	@RequiresPermissions("module:userWalletIn:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(UserWalletIn userWalletInReq) {
		startPage();
        List<UserWalletIn> list = userWalletInService.selectUserWalletInList(userWalletInReq);
		for (UserWalletIn userWalletIn : list) {
			String name = userWalletIn.getUser().getNickName();
			if (StringUtils.isNotEmpty(name)) {
				userWalletIn.setNickName(name);
			}
			String phone = userWalletIn.getUser().getPhone();
			if (StringUtils.isNotEmpty(phone)) {
				userWalletIn.setPhone(phone);
			}
		}

		return getDataTable(list);
	}

	/**
	 * 新增充值
	 */
	@GetMapping("/add")
	public String add() {
	    return prefix + "/add";
	}

	/**
	 * 新增保存充值
	 */
	@RequiresPermissions("module:userWalletIn:add")
	@Log(title = "充值", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(UserWalletIn userWalletIn) {
        userWalletIn.setCreateBy(ShiroUtils.getLoginName());
		return toAjax(userWalletInService.insertUserWalletIn(userWalletIn));
	}

	/**
	 * 修改充值
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
		UserWalletIn userWalletIn = userWalletInService.selectUserWalletInById(id);
		mmap.put("userWalletIn", userWalletIn);
	    return prefix + "/edit";
	}

	/**
	 * 修改保存充值
	 */
	@RequiresPermissions("module:userWalletIn:edit")
	@Log(title = "充值", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(UserWalletIn userWalletIn) {
        userWalletIn.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(userWalletInService.updateUserWalletIn(userWalletIn));
	}

	/**
	 * 删除充值
	 */
	@RequiresPermissions("module:userWalletIn:remove")
	@Log(title = "充值", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(userWalletInService.deleteUserWalletInByIds(ids));
	}

}
